package com.lutzyt.delivery.controller;

import com.lutzyt.delivery.bean.Customer;
import com.lutzyt.delivery.bean.Horseman;
import com.lutzyt.delivery.bean.Menu;
import com.lutzyt.delivery.bean.User;
import com.lutzyt.delivery.bean.vo.MenuTree;
import com.lutzyt.delivery.remote.WxService;
import com.lutzyt.delivery.service.CustomerService;
import com.lutzyt.delivery.service.HorsemanService;
import com.lutzyt.delivery.service.MenuService;
import com.lutzyt.delivery.service.UserService;
import com.lutzyt.delivery.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author zyt
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class LoginController {

  private static final Integer EMPTY = 0;
  private UserService userService;
  private CustomerService customerService;
  private HorsemanService horsemanService;
  private MenuService menuService;
  private WxService wxService;

  @GetMapping("/home")
  public Result<List<MenuTree>> home(HttpSession session) {
    log.info("登录成功,获取权限信息！");
    SecurityContextImpl securityContext = (SecurityContextImpl) session.getAttribute("SPRING_SECURITY_CONTEXT");
    final String username = ((UserDetails) securityContext.getAuthentication().getPrincipal()).getUsername();
    final User user = userService.findByName(username);
    session.setAttribute("user", user);
    session.setAttribute("userId", user.getId());
    List<MenuTree> menus = menuService.getByUsername(username);
    return Result.success(menus);
  }

  @PostMapping("/customer/login")
  public Result<Integer> customerLogin(@RequestParam("code") String code) {
    log.info("客户端微信小程序登录:code:{}", code);
    final String customerOpenId = wxService.getCustomerOpenId(code);
    log.info("openId: {}", customerOpenId);
    final Customer customer = customerService.query().eq("wx_openid", customerOpenId).one();
    if (customer == null) {
      Integer id = customerService.register(customerOpenId);
      return Result.success(id).setCode(2001);
    }
    log.info("customer:{}", customer);
    if (EMPTY.equals(customer.getStatus())) {
      return Result.success(customer.getId()).setCode(2002);
    }
    return Result.success(customer.getId());
  }

  @PostMapping("/horseman/login")
  public Result<Integer> horsemanLogin(@RequestParam("code")String jsCode) {
    final String horsemanOpenId = wxService.getHorsemanOpenId(jsCode);
    final Horseman horseman = horsemanService.query().eq("wx_openid", horsemanOpenId).one();
    log.info("骑手端微信小程序登录:js_code{}", jsCode);
    if (horseman == null) {
      Integer id = horsemanService.register(horsemanOpenId);
      return Result.success(id).setCode(2001);
    }
    log.info("horseman:{}", horseman);
    if (EMPTY.equals(horseman.getStatus())) {
      return Result.success(horseman.getId()).setCode(2002);
    }
    return Result.success(horseman.getId());
  }


  @Autowired
  public void setMenuService(MenuService menuService) {
    this.menuService = menuService;
  }

  @Autowired
  public void setCustomerService(CustomerService customerService) {
    this.customerService = customerService;
  }

  @Autowired
  public void setHorsemanService(HorsemanService horsemanService) {
    this.horsemanService = horsemanService;
  }

  @Autowired
  public void setWxService(WxService wxService) {
    this.wxService = wxService;
  }

  @Autowired
  public void setUserService(UserService userService) {
    this.userService = userService;
  }


}
